package com.example.demo.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.RemoteEndpoint.Basic;
import javax.websocket.Session;

/*
 * 工具类：用来向所有客户端发送消息
 */
public class WebSocketUtil {
	//存放当前在聊天室的人
	public static final Map<String,Session> sessionmap = new ConcurrentHashMap<>();
	public static void sendMessage(Session session,String message){
	if(session!=null){
		Basic basic = session.getBasicRemote();
		if(basic!=null){
			try {
				//向客户端发消息
				basic.sendText(message);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	}
	
	//发送所有人，遍历map给所有人发消息
	public static void sendAll(String message){
		sessionmap.forEach((sessionId,session)->sendMessage(session, message));
		//List<String> list = new ArrayList<>();
		
		
	}
}
